import localforage from 'localforage';

// 配置 localforage
localforage.config({
  name: 'bcgm',
  storeName: 'herbs',
  description: '本草纲目数据存储'
});

// 定义存储键
const STORAGE_KEYS = {
  HERBS: 'herbs',
  RELATIONS: 'relations',
  MODELS: 'models',
  SETTINGS: 'settings'
} as const;

// 存储接口
interface StorageService {
  // 药材数据
  getHerbs(): Promise<any[]>;
  setHerbs(herbs: any[]): Promise<void>;
  
  // 关系数据
  getRelations(): Promise<any[]>;
  setRelations(relations: any[]): Promise<void>;
  
  // 3D模型
  getModel(name: string): Promise<Blob | null>;
  setModel(name: string, model: Blob): Promise<void>;
  
  // 设置
  getSettings(): Promise<any>;
  setSettings(settings: any): Promise<void>;
}

// 实现存储服务
export const storage: StorageService = {
  // 药材数据
  async getHerbs() {
    return await localforage.getItem(STORAGE_KEYS.HERBS) || [];
  },
  
  async setHerbs(herbs) {
    await localforage.setItem(STORAGE_KEYS.HERBS, herbs);
  },
  
  // 关系数据
  async getRelations() {
    return await localforage.getItem(STORAGE_KEYS.RELATIONS) || [];
  },
  
  async setRelations(relations) {
    await localforage.setItem(STORAGE_KEYS.RELATIONS, relations);
  },
  
  // 3D模型
  async getModel(name) {
    return await localforage.getItem(`${STORAGE_KEYS.MODELS}_${name}`);
  },
  
  async setModel(name, model) {
    await localforage.setItem(`${STORAGE_KEYS.MODELS}_${name}`, model);
  },
  
  // 设置
  async getSettings() {
    return await localforage.getItem(STORAGE_KEYS.SETTINGS) || {};
  },
  
  async setSettings(settings) {
    await localforage.setItem(STORAGE_KEYS.SETTINGS, settings);
  }
}; 